import 'package:flutter/material.dart';
import 'package:steam_app/config/app_theme.dart';
import '../../config/app_routes.dart';
import '../../entity/game.dart';
import '../../entity/product.dart';

class GamePromoCard extends StatelessWidget {
  final Game game;

  const GamePromoCard({super.key, required this.game});

  @override
  Widget build(BuildContext context) {
    //从游戏的产品列表中找到基础版游戏
    //使用firstWhere查找，如果找不到则返回null
    final Product? baseProduct = game.products.cast<Product?>().firstWhere(
          (p) => p?.type == 'BASE_GAME',
      orElse: () => null,
    );
    //如果找不到基础版，就随便取第一个产品作为后备
    final Product? displayProduct = baseProduct ?? (game.products.isNotEmpty ? game.products.first : null);


    return InkWell(
      onTap: () {
        Navigator.of(context).pushNamed(
            gameDetail_page,
            arguments: game,
        );
      },
      child: Card(
        margin: const EdgeInsets.symmetric(horizontal: hPaddingSize, vertical: vPaddingSize),
        clipBehavior: Clip.antiAlias, //图片圆角
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //游戏海报
            Image.asset(
              game.coverImage,
              fit: BoxFit.cover,
              height: gameCoverHeight,
              width: double.infinity,

              errorBuilder: (context, error, stackTrace) {
                return const SizedBox(
                  height: gameCoverHeight,
                  child: Center(child: Icon(Icons.error)),
                );
              },
            ),

            //游戏信息
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    game.name,
                    style: const TextStyle(fontSize: gameCardTitleFontSize, fontWeight: titleFontWeight),
                  ),
                  const SizedBox(height: 8),
                  Text('来自开发商：${game.developer}'),
                  const SizedBox(height: priceTagHeight),

                  //价格和折扣信息
                  Align(
                    alignment: Alignment.centerRight,
                    //如果能找到可供显示的产品，则显示价格，否则不显示
                    child: displayProduct != null
                        ? Container(
                      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                      decoration: BoxDecoration(
                        color: priceTagBackgroundColor,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text(
                        '¥ ${displayProduct.price.toStringAsFixed(2)}',
                        style: const TextStyle(
                          color: priceTagTextColor,
                          fontWeight: titleFontWeight,
                          fontSize: 16,
                        ),
                      ),
                    )
                        : const SizedBox.shrink(), //如果没有价格，则显示一个空的小部件
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}